<?php


namespace app\model\cart;


use app\model\coach\UserCoachModel;
use think\Model;

class VerifyCartModel extends Model
{
    protected $name = 'verify_cart';
    protected $autoWriteTimestamp = true;

    protected $append = ['shape_id_txt', 'pay_status_txt'];

    protected $statusArr = [
        -1 => '支付失败',
        1 => '待支付',
        2 => '服务中',
        3 => '已完成',
        4 => '已取消'
    ];

    public function setTimeAttr($value)
    {
        return strtotime($value);
    }

    public function getTimeAttr($value)
    {
        return date('Y-m-d H:i', $value);
    }

    public function getPayTimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d H:i', $value) : '';
    }

    public function getCancelTimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d H:i', $value) : '';
    }

    public function coach()
    {
        return $this->hasOne(UserCoachModel::class, 'id', 'coach_user_id')->bind([
            'coach_avatar' => 'avatar',
            'coach_username' => 'username',
            'coach_is_auth' => 'is_auth',
            'coach_age' => 'age',
            'coach_drive_year' => 'drive_year',
        ]);
    }

    public function getShapeIdTxtAttr($value, $data)
    {
        if (!isset($data['shape_id'])) {
            return [];
        }

        return VerifyCartShapeModel::whereIn('id', explode(',', $data['shape_id']))->column('title');
    }

    public function getPayStatusTxtAttr($value, $data)
    {
        if (!isset($data['pay_status'])) {
            return [];
        }
        return $this->statusArr[$data['pay_status']];
    }

    public function getStatusArr()
    {
        return $this->statusArr;
    }
}